package net.kazed.nextaction.timer;

import net.kazed.nextaction.R;
import net.kazed.nextaction.database.Task;
import android.app.AlarmManager;
import android.app.Dialog;
import android.app.PendingIntent;
import android.content.ContentUris;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

/**
 * Dialog for timer.
 * @author Koert Zeilstra
 */
public class TimerDialog extends Dialog {

   private SharedPreferences preferences;
   private EditText fieldDuration;
   private int taskId;
   private Notifier notifier;

   /**
    * Constructor.
    * @param context Android context.
    */
   public TimerDialog(Context context) {
      super(context);
      preferences = PreferenceManager.getDefaultSharedPreferences(context);
      notifier = new Notifier(context);
   }

   /**
    * @param taskId Task ID.
    */
   public void setTaskId(int taskId) {
      this.taskId = taskId;
   }

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.timer_dialog);
      setTitle(R.string.timer_dialog_title);

      fieldDuration = (EditText) findViewById(R.id.timer_duration);
      
      Button okButton = (Button) findViewById(R.id.ok_button);
      okButton.setOnClickListener(new android.view.View.OnClickListener() {
         public void onClick(View view) {
            startTimer();
            dismiss();
         }
      });

      Button cancelButton = (Button) findViewById(R.id.cancel_button);
      cancelButton.setOnClickListener(new android.view.View.OnClickListener() {
         public void onClick(View view) {
            dismiss();
         }
      });

      String timerDuration = preferences.getString("timer_duration", "25");
      fieldDuration.setText(timerDuration);
   }

   private void startTimer() {
      String duration = fieldDuration.getText().toString();
      Editor editor = preferences.edit();
      editor.putString("timer_duration", duration);
      editor.commit();
      
      long alarmTime = System.currentTimeMillis() + Integer.parseInt(duration) * 60000;

      Context context = getContext();
      AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
      Uri timerTaskUri = ContentUris.withAppendedId(Task.CONTENT_TIMER_URI, taskId);

      Intent processIntent = new Intent(Intent.ACTION_VIEW, timerTaskUri);
      PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 0, processIntent, 0);
      alarmManager.set(AlarmManager.RTC_WAKEUP, alarmTime, alarmIntent);
      
      Uri taskUri = ContentUris.withAppendedId(Task.CONTENT_URI, taskId);
      Intent notificationIntent = new Intent(Intent.ACTION_VIEW, taskUri);
      notifier.timerStart(notificationIntent);
   }
   
}
